package com.prism.e4.rcp.ui.handlers;

import java.util.List;

import javax.inject.Named;

import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.e4.ui.workbench.modeling.EPartService;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

//adapt to pepa file
public class NewPEPAModelHandler2 {

	//SupportedFile supportedFile;
	String id;

	@Execute
	public void execute(
			@Named(IServiceConstants.ACTIVE_SHELL) Shell parentShell,
			MApplication application, EModelService modelService,
			final EPartService partService, final IWorkspace workspace,
			final IProgressMonitor monitor) {

		TitleAreaDialog dialog = new TitleAreaDialog(parentShell) {
			private Text fileName;
			private Text fileLocation;

			@Override
			protected int getShellStyle() {
				return super.getShellStyle() | SWT.SHEET;
			}

			@Override
			protected Control createDialogArea(Composite parent) {
				Composite comp = (Composite) super.createDialogArea(parent);
				getShell().setText("New Prism Model");
				setTitle("Prism model properties");
				setMessage("Please enter the name of new prism model");
				getShell().addDisposeListener(new DisposeListener() {
					public void widgetDisposed(DisposeEvent e) {
						// disposing images
					}
				});

				Composite container = new Composite(comp, SWT.NONE);
				container.setLayoutData(new GridData(GridData.FILL_BOTH));
				container.setLayout(new GridLayout(3, false));
				Label fileNameLabel = new Label(container, SWT.NONE);
				fileNameLabel.setText("Prism model name:");

				fileName = new Text(container, SWT.BORDER);
				fileName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
				new Label(container, SWT.NONE);

				Label fileLocationLabel = new Label(container, SWT.NONE);
				fileLocationLabel.setText("Location:");

				fileLocation = new Text(container, SWT.BORDER);
				fileLocation.setLayoutData(new GridData(
						GridData.FILL_HORIZONTAL));

				String xdgConfigHome = System.getenv("XDG_CONFIG_HOME");
				if (xdgConfigHome != null) {
					fileLocation.setText(xdgConfigHome);
				} else {
					fileLocation.setText(System.getProperty("user.home"));
				}

				Button button = new Button(container, SWT.PUSH);
				button.setText("Browse...");
				button.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent event) {
						DirectoryDialog dlg = new DirectoryDialog(getShell());
						dlg.setFilterPath(fileLocation.getText());
						dlg.setText("Select model's directory");
						dlg.setMessage("Select a directory");
						String directory = dlg.open();
						if (directory != null) {
							fileLocation.setText(directory);
						}
					}
				});

				return comp;
			}

			@Override
			protected void okPressed() {
				if (fileName.getText().trim().length() == 0) {
					setMessage("Please enter a model name",
							IMessageProvider.ERROR);
					return;
				}
				if (fileLocation.getText().trim().length() == 0) {
					setMessage("Please enter a model location",
							IMessageProvider.ERROR);
					return;
				}

				// create prismFile with id equaled to 0
				// TODO: change to pepa file... when created
				//supportedFile = new SupportedFile();
				//supportedFile.setFileName(fileName.getText());
				//supportedFile.setFileExtension(".pepa");
				// for a while text is including file path
				//supportedFile.setText(fileLocation.getText());
				// TODO: add IFile and IPath to prismFile

				// IFile file;
				// System.out.println("Vytvorit v adresari:"
				// + prismFile.toString());

				// ByteArrayInputStream out = new ByteArrayInputStream(new
				// byte[0]);
				// try {
				// if (monitor != null) {
				// file.create(out, true, monitor);
				// }
				// } catch (CoreException e) {
				// e.printStackTrace();
				// }
				// try {
				// file.create(out, true, monitor);
				// } catch (CoreException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				super.okPressed();
			}
		};

		if (dialog.open() == IDialogConstants.OK_ID) {

			//SupportedFile fileToSave = FileServiceFactory.getInstance()
			//		.createEmptyFile();
			//fileToSave.setFileName(supportedFile.getFileName());
			//fileToSave.setFileExtension(supportedFile.getFileExtension());
			//fileToSave.setText(supportedFile.getText());
			//FileServiceFactory.getInstance().saveFile(fileToSave);

			//if (supportedFile == null) {
			//	return;
			//}

			//id = String.valueOf(fileToSave.getId());
			System.out.println("NewPrismModelHandler PrismFileId:" + id);
		} else {
			return;
		}

		List<MPart> parts = (List<MPart>) partService.getParts();

		for (MPart mPart : parts) {
			//String currentId = mPart.getPersistedState().get(
			//		SupportedFile.FIELD_ID);
			//if (currentId != null && currentId.equals(id)) {
			//	partService.showPart(mPart, PartState.ACTIVATE);
			//	return;
			}
		}

		//MPart part = modelService.createModelElement(MPart.class);
		//part.setContributionURI("bundleclass://com.prism.e4.rcp"
		//		+ "/com.prism.e4.rcp.parts.E4PrismEditor");
		//part.getPersistedState().put(SupportedFile.FIELD_ID, id);

		//String header = supportedFile.getFileName()
		//		+ supportedFile.getFileExtension();
		//part.setLabel(header);
		//part.setElementId(id);
		//part.setCloseable(true);

		//MPartStack stack = (MPartStack) modelService.find(
		//		"com.prism.e4.rcp.partstack.editor", application);
		//stack.getChildren().add(part);
		//partService.showPart(part, PartState.ACTIVATE);
	//}
}

